import 'package:flutter/material.dart';
import 'package:myapp1/service/wordtime.dart';

// 知识点: 列表布局、路由传参Navigator.pop
// 创建有状态Widget ChooseLocation

class ChooseLocation extends StatefulWidget {
  const ChooseLocation({super.key});

  @override
  State<ChooseLocation> createState() => _ChooseLocationState();
}

class _ChooseLocationState extends State<ChooseLocation> {
  List<WordTime> locations = [
    WordTime(url: "Shanghai", location: "上海", flag: "china.png"),
    WordTime(url: "Seoul", location: "韩国", flag: "korea.png"),
  ];

  void updateTime(index) async {
    WordTime instance = locations[index];
    await instance.getTime();
    Navigator.pop(context, {
      'location': instance.location,
      'flag': instance.flag,
      'time': instance.time,
      'isDaytime': instance.isDaytime
    });
  }


  @override
  Widget build(BuildContext context) {
    // 获取路由传过来的参数
    var params = ModalRoute.of(context)!.settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text(
            "选择位置",
            style: TextStyle(
              fontSize: 20.0,
              color: Colors.white
            )
        ),
        centerTitle: true,
        backgroundColor: Colors.blue[800],
        // 设置返回按钮为白色
        iconTheme: IconThemeData(
          color: Colors.white
        ),
      ),
      body: ListView.builder(
        itemCount: locations.length,
        itemBuilder: (context, index) {
          return Padding(
            padding: const EdgeInsets.symmetric(vertical: 1.0, horizontal: 4.0),
            child: Card(
              child: ListTile(
                onTap: () {
                  updateTime(index);
                },
                title: Text(locations[index].location as String),
                leading: CircleAvatar(
                  backgroundImage: AssetImage('assets/images/2.png'),
                  // backgroundImage: AssetImage('assets/images/${locations[index].flag}'),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
